# coding=utf-8
import Crack
from flask import jsonify
from flask import request
from flask import Flask
from flask import render_template
from flask_cors import *

app = Flask(__name__)
# 跨域设置
CORS(app, supports_credentials=True)


@app.route('/', methods=['POST', 'GET'])
def kws():
    if request.method == "GET":
        return render_template('index.html', title='天眼查字符破解api')
    elif request.method == 'POST':
        res_dict = request.get_json(' ')
        if isinstance(res_dict, (dict,)):
            if 'key' in res_dict.keys() and 'lock' in res_dict.keys():
                key = res_dict['key']
                lock = res_dict['lock']
                try:
                    response = Crack.crack(key, lock)
                    return jsonify({'status_code': 200, 'value': response})
                except:
                    return jsonify({'status_code': 500, 'value': '服务器异常'})
            else:
                return jsonify({'status_code': 400, 'value': '未检测到key与lock字段'})
        else:
            return jsonify({'status_code': 400, 'value': '提交参数非json模式'})
    else:
        return jsonify({'status_code': 400, 'value': '只接受Get与Post请求'})


if __name__ == '__main__':
    app.run(
        host='127.0.0.1',
        port=8080,
        debug=False
    )
